package com.xmtlxx.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.domain.entity.SysCheckItem;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.system.domain.bo.SysCheckItemBo;
import com.xmtlxx.system.domain.bo.SysTagCheckitemBo;
import com.xmtlxx.system.domain.vo.SysCheckItemVo;
import com.xmtlxx.system.domain.vo.SysTagCheckitemVo;
import com.xmtlxx.system.service.ISysCheckItemService;
import com.xmtlxx.system.service.ISysTagCheckitemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 检查项Controller
 *
 * @author xmtlxx
 * @date 2022-04-26
 */
@Validated
@Api(value = "检查项控制器", tags = {"检查项管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/sysCheckItem")
public class SysCheckItemController extends BaseController {

    private final ISysCheckItemService iSysCheckItemService;

    private final ISysTagCheckitemService iSysTagCheckitemService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<SysCheckItemVo> initBean(){
        SysCheckItemVo vo = new SysCheckItemVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询检查项列表
     */
    @ApiOperation("查询检查项列表")
    @SaCheckPermission("system:sysCheckItem:list")
    @GetMapping("/list")
    public TableDataInfo<SysCheckItemVo> list(@Validated(QueryGroup.class) SysCheckItem sysCheckItem, PageQuery pageQuery) {
        return iSysCheckItemService.queryPageList(sysCheckItem, pageQuery);
    }

    /**
     * 导出检查项列表
     */
    @ApiOperation("导出检查项列表")
    @SaCheckPermission("system:sysCheckItem:export")
    @Log(title = "检查项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysCheckItemBo bo, HttpServletResponse response) {
        List<SysCheckItemVo> list = iSysCheckItemService.queryList(bo);
        ExcelUtil.exportExcel(list, "检查项", SysCheckItemVo.class, response);
    }

    /**
     * 获取检查项详细信息
     */
    @ApiOperation("获取检查项详细信息")
    @SaCheckPermission("system:sysCheckItem:query")
    @GetMapping("/{itemId}")
    public R<Map<String, Object>> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("itemId") Long itemId) {
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("item",iSysCheckItemService.queryById(itemId));
        SysTagCheckitemBo sysTagCheckitemBo = new SysTagCheckitemBo();
        sysTagCheckitemBo.setItemId(itemId);
        List<SysTagCheckitemVo> tags = iSysTagCheckitemService.queryList(sysTagCheckitemBo);
        ajax.put("tagIds",tags.stream().map(SysTagCheckitemVo::getTagId).collect(Collectors.toList()));
        return R.ok(ajax);
    }

    /**
     * 新增检查项
     */
    @ApiOperation("新增检查项")
    @SaCheckPermission("system:sysCheckItem:add")
    @Log(title = "检查项", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysCheckItemVo vo) {
        SysCheckItemBo bo = new SysCheckItemBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysCheckItemService.insertByBo(bo,vo.getTagIds()) ? 1 : 0);
    }

    /**
     * 修改检查项
     */
    @ApiOperation("修改检查项")
    @SaCheckPermission("system:sysCheckItem:edit")
    @Log(title = "检查项", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysCheckItemVo vo) {
        SysCheckItemBo bo = new SysCheckItemBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysCheckItemService.updateByBo(bo,vo.getTagIds()) ? 1 : 0);
    }

    /**
     * 删除检查项
     */
    @ApiOperation("删除检查项")
    @SaCheckPermission("system:sysCheckItem:remove")
    @Log(title = "检查项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{itemIds}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] itemIds) {
        return toAjax(iSysCheckItemService.deleteWithValidByIds(Arrays.asList(itemIds), true) ? 1 : 0);
    }

    /**
     * 状态修改
     */
    @ApiOperation("状态修改")
    @SaCheckPermission("system:sysCheckItem:edit")
    @Log(title = "检查项", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody SysCheckItemBo bo) {
        return toAjax(iSysCheckItemService.updateByBo(bo,null));
    }
}
